package com.test.controller;


import com.test.service.MvtService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@CrossOrigin
@RequestMapping("/map/vectortile")
@Tag(name="mvt api")
public class MvtController {

    @Autowired
    MvtService service;
    @Operation(summary = "上传文档")
    @GetMapping("/{layer}/{z}/{x}/{y}.pbf")
    public void vectorTitle2(@PathVariable("layer") String layer, @PathVariable("z") Integer z, @PathVariable("x") Integer x, @PathVariable("y") Integer y, HttpServletResponse response) {
        response.setContentType("application/x-protobuf;type=mapbox-vector;chartset=UTF-8");
        byte[] tile = service.vectorTitle(z, x, y, layer);
        // 输出文件流
        OutputStream os = null;
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(tile);
            os = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
